<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * @ID Model_city.php
 * 城市model
 * @date 2013-2-17
 * @author yuhailong zjj008@gmail.com
 * @copyright zhuna Inc , all rights reserved
 * 
 */

class Model_city extends CI_Model
{
	private $cachePath ;
	
	function __construct()
	{
        parent::__construct();
        
        $this->cachePath = ROOTPATH.DIRECTORY_SEPARATOR.$this->config->config['cache_path']."list/";//缓存存放目录
    }
    
    /**
     * 得到热门城市
     * @param $int $num  得到城市的数目
     * @return array $hotCArray 热门城市列表
     */
    public function getHotCity($num = 12)
    {
        $cityList = $this->getCity();
        $i = 0;
        foreach($cityList as $key=>$val) {
            $i++;
            if($i>$num){
                break;    
            }
            $hotCArray[] = array('cid'=>$val['cid'],'pid'=>$val['pid'],'cName'=>$val['cName'],'abcd'=>$val['abcd']);
        }
        return $hotCArray; 
    }
    
    /**
     * 根据pid得到城市
     * @param $int $pid  省份id
     * @return array $hotCArray 热门城市列表
     */
    public function getCityByPid($pid)
    {
        $reqdata = $this->getCity();
        $cityArr = array();
        foreach($reqdata as $key=>$val) {
            if($val['pid'] == $pid){   
                $cityArr[] = array('cid'=>$val['cid'],'cName'=>$val['cName']);
            }
        }
        return  $cityArr;   
    }
    
    /**
     * 得到ABCD为索引的城市列表
     * @return array $cityArray 城市列表
     */
    public function getCityToABCD()
    {
        $reqdata = $this->getCity();
        $cityArray = $this->getABCD();
        foreach($reqdata as $key=>$val) {
            $oneCity = array('cid'=>$val['cid'],'pid'=>$val['pid'],'cName'=>$val['cName'],'abcd'=>$val['abcd']);
            $cityArray[$val['abcd']][] = $oneCity ;  
        }
        foreach($cityArray as $key=>$val) {
            if(empty($val)){
                unset($cityArray[$key]);    
            }
        }
        return  $cityArray;
    }
    
    /**
     * 得到城市列表
     * @return array $cityList 城市列表
     */
    public function getCity()
    {
        $cacheName = 'city.json';
        $cityJSON =json_decode(file_get_contents($this->cachePath.$cacheName),true);  
        return $cityList = $cityJSON['reqdata'];    
    }
        
    /**
     * 构造ABCD的数组
     * @return array $cityList 城市列表
     */
    public function getABCD()
    {
        return $ABCD = array("A"=>'',"B"=>'',"C"=>'',"D"=>'',"E"=>'',"F"=>'',"G"=>'',"H"=>'',"I"=>'',"J"=>'',"K"=>'',"L"=>'',"M"=>'',"N"=>'',"O"=>'',"P"=>'',"Q"=>'',"R"=>'',"S"=>'',"T"=>'',"U"=>'',"V"=>'',"W"=>'',"X"=>'',"Y"=>'',"Z"=>'');       
    }
    
    /**
     * @author zhaojianjun
     * 得到城市列表
     * @param $cityid 
     * @return array $city 城市id
     */
    public function get_local_city_byid($cityid)
    {
        $cityList = $this->model_city->getCity();
        foreach ($cityList as $key => $city){
        	if($cityid == $city['cid']){
        		return $city;
        	}
        } 
    }    
    /**
     * 得到全国省份列表
     * @return <array> $cityinfo 
     */
    function getProvinceList($pid = '')
    {
        //得到省份数组
        $cacheName = 'eprovince.json';
        $provinceList =json_decode(file_get_contents($this->cachePath.$cacheName),true);
        foreach($provinceList as $key=>$val){
            $pid == $val['province_id'] ? $provinceList[$key]['class'] = 'current' : $provinceList[$key]['class'] = '';   
        } 
        return  $provinceList;  
    }
    
    /**
     * 得到默认城市
     * @return <array> $cityinfo 
     */
    function getInitCityList()
    {
        $cityArr = explode(',',CFG_INDEXCITYLIST); 
        $rt = array();
        foreach ($cityArr as $val) {
            $oneCity = explode('|',$val); 
            $rt[] = array('cityid'=>$oneCity[0],'cityname'=>$oneCity[1]);           
        }
        return $rt;
    }
    
}
?>